package com.lc.reflect;

import com.lc.demo.User;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectTest {

    public static void testNew() {
        long starTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            User user = new User();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("newTime: " + (endTime - starTime));
    }

    public static void testForNameNew() throws Exception {
        long starTime = System.currentTimeMillis();
        Class<?> userClass = Class.forName("com.lc.demo.User");
        for (int i = 0; i < 100000; i++) {
            User user = (User) userClass.newInstance();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("classNewTime: " + (endTime - starTime));
    }

    public static void testConstructor() throws Exception {
        long starTime = System.currentTimeMillis();
        Class<?> userClass = Class.forName("com.lc.demo.User");
        Constructor<?> constructor = userClass.getConstructor();
        System.out.println("constructor: " + constructor);
        for (int i = 0; i < 100000; i++) {
            User user = (User) constructor.newInstance();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("classNewTime: " + (endTime - starTime));
    }

    public static void testSet() {
        long starTime = System.currentTimeMillis();
        User user = new User();
        for (int i = 0; i < 100000; i++) {
            user.setId(1);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("newTime: " + (endTime - starTime));
    }


    public static void testMethodSet() throws Exception {
        User user = new User();
        long starTime = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++) {
            Method setName = User.class.getMethod("setName", String.class);
            setName.invoke(user, "ss");
        }
        long endTime = System.currentTimeMillis();
        System.out.println("newTime: " + (endTime - starTime));
    }

    public static void testField() throws Exception {
        User user = new User();
        long starTime = System.currentTimeMillis();

        for (int i = 0; i < 100000; i++) {
            Field name = User.class.getDeclaredField("name");
            name.setAccessible(true);
            name.set(user, "ss");
        }
        long endTime = System.currentTimeMillis();
        System.out.println("newTime: " + (endTime - starTime));
    }

    public static void main(String[] args) throws Exception {
        // testNew(); // 8  100000次new 对象，5次实验取中位数
        /**
         * 150(forName 放入for循环的情况下)  100000次forName 对象，5次实验取中间值，forName
         * 放出来的话 时间在15-28 徘徊
         */
        //testForNameNew();
        //testConstructor(); //28-38  100000次constructor.newInstance 对象 5次实验取中位数

        //testSet(); //0-2

        testMethodSet(); //20-41， 在getMethod 方法不及时的情况下，  getMethod 方法计时的情况下，任然为20-43，基本无区别。 在getMethod 方法在循环内的情况下为140-170 之间

        //testField(); //17-28(在获取field 后在做循环的情况下) 中间。 在field 在循环内部的时候 时间在79-100 中间
    }
}
